﻿// ====================================================================================================================
// Project: Camstar InSiteExtractor
// Source File: InSiteExtractor.cs
// Author: Roop Sin LE
// Created(MM/DD/YYYY HH:MM): 08/13/2010 9:14:21 PM
// Copyright: Camstar Inc.
// ====================================================================================================================

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using InSiteExtractor.Definition;
using log4net;

namespace InSiteExtractor
{
    class StatusMonitor
    {
        // Define a static logger variable so that it references the
        private static readonly ILog Log = LogManager.GetLogger(typeof(StatusMonitor));

        private TimeSpan _interval;
        private DateTime _lastActivityDate;
        private int _workNumber;
        private int _workCompleted;
        private string _workMessage;

        #region Properties
        public int WorkNumber
        {
            set
            {
                _workNumber = value;
                _workCompleted = 0;
                UpdatedItemNumber = 0;
                _workMessage = string.Format("Start processing {0} files", _workNumber);
                PrintOut();
            }
            get { return _workNumber; }
        }

        public int UpdatedItemNumber { set; get; }

        private string _fieldProcessing;

        public string FieldProcessing
        {
            set
            {
                _fieldProcessing = value;
                _workMessage = string.Format("Processing the file [{0}] - {1}/{2} file[s]", _fieldProcessing, _workCompleted + 1, _workNumber);
                PrintOut();
            }
            get { return _fieldProcessing; }
        }

        public int WorkCompleted
        {
            set
            {
                _workCompleted = value;
                _workMessage = string.Format("Completed! Rows updated = {0} on file  the file [{1}]- {2}/{3} file[s] - {4:P}", UpdatedItemNumber, _fieldProcessing, _workCompleted, _workNumber, (double)_workCompleted / _workNumber);
                UpdatedItemNumber = 0;
                PrintOut();
            }
            get { return _workCompleted; }
        }

        #endregion

        public StatusMonitor()
        {
            _interval = new TimeSpan(0, 0, 0, 0, int.Parse(ConfigurationManager.AppSettings[ExtractorConst.MonitorInterval]));
            _lastActivityDate = DateTime.Now;
        }

        public void UpdateStatus(int row, string columnName)
        {
            if (CheckToPrint())
            {
                _workMessage = string.Format("{0} - Processing at row {1} & column {2}",_fieldProcessing, row, columnName);
                PrintOut();
            }
        }

        private bool CheckToPrint()
        {
            return (DateTime.Now.Subtract(_interval) > _lastActivityDate);
        }

        private void PrintOut()
        {
            Log.Info(_workMessage);
            _lastActivityDate = DateTime.Now;
        }
    }
}
